local SkillBehavior = require("gameLogic.fightSkill.show.SkillBehavior")
local SkillMove = class("SkillMove", SkillBehavior)
local AnimConst = require("const.AnimConst")
local GameUtils = require("utils.GameUtils")
local cs_coroutine = require("utils.cs_corutine")
local FightHelper = require("gameLogic.fight.FightHelper")
-- 移动到目标面前释技能

local DISTANCE = 3.2

function SkillMove:ctor(config)
    SkillMove.super.ctor(self, config)
end

function SkillMove:_getEnemyEntityCenterPosition(targets)
    local targetTransform = targets[1].transform
    local targetPosition = targetTransform.position + targetTransform.forward * DISTANCE
    return targetPosition
end

function SkillMove:attack(index)
    if not self._entity then
        return
    end
    local targets = self:getSkillTargets(index)
    if not targets then
        return
    end
    local targetPosition = self:_getEnemyEntityCenterPosition(targets)

    local entityTransform = self._entity.transform
    local startPosition = entityTransform.position

    local waittime = GameUtils.getAnimationLength(self._entity:getAnimator(), self:getSkillAction())
    local hitTime = self:getHitTime()
    local stragyTime = self:getEffectTime()

    local sequence = DOTween.Sequence()
    local goTarget = entityTransform:DOMove(targetPosition, 0.5)

    local oldEulerAngles = self._entity:getRoot().localEulerAngles

    local goLookAt = self._entity:getRoot():DOLookAt(targets[1].transform.position, 0.2)

    local goAttackFunc = function()
        self._entity:setAnimBoolean(AnimConst.Moving, false)
        self._entity:triggerAnim(self:getSkillAction() .. "Trigger")
    end

    local goBeAttackFunc = function()
        self:strategy(targets)
    end
    local goMoveFunc = function()
        self._entity:setAnimBoolean(AnimConst.Moving, true)
    end
    local goBack = entityTransform:DOMove(startPosition, 0.5)
    local goBackFunc = function()
        self._entity:setAnimBoolean(AnimConst.Moving, false)
    end

    local goRotate = self._entity:getRoot():DOLocalRotate(oldEulerAngles, 0.2)

    local goDone = function()
        self._entity:setDone(true)
    end

    sequence:AppendCallback(goMoveFunc)
    sequence:Append(goTarget)
    sequence:Append(goLookAt)
    sequence:AppendCallback(goAttackFunc)
    sequence:AppendInterval(hitTime)
    sequence:AppendCallback(goBeAttackFunc)
    if stragyTime + hitTime > waittime then
        sequence:AppendInterval(stragyTime)
    else
        sequence:AppendInterval(waittime - hitTime - stragyTime)
    end
    sequence:AppendCallback(goMoveFunc)
    sequence:Append(goBack)
    sequence:AppendCallback(goBackFunc)
    sequence:AppendInterval(0.2)
    sequence:Append(goRotate)
    sequence:AppendCallback(goDone)
end

return SkillMove
